capture log close
log using ${ac}\energy_insecurity, replace

import spss using "C:\RA\INDU0005_OUTPUT_national.sav", clear

rename q1 hhsize
rename q2_2 hh_under5

gen hh_over65 =0
replace hh_over65=1 if q2_3==2
replace hh_over65=1 if q2_3==3
replace hh_over65=1 if q2_3==4
replace hh_over65=1 if q2_3==5
label define hh_over65_label 0 "0" 1 "1" 2 "2" 3 "3" 4 "more than 3"
label val hh_over65 hh_over65_label


rename q2_4 hh_disability
rename q38_1 hh1_income
rename q38_2 hh2_income
rename q38_3 hh3_income
rename q38_4 hh4_income
rename q38_5 hh5_income
rename q38_6 hh6_income
rename q38_7 hh7_income
rename q38_8 hh8_income
rename q38_all income


gen dwelling_type=0
replace dwelling_type=1 if q5 ==1
replace dwelling_type=2 if q5 ==2
replace dwelling_type=3 if q5 ==4
replace dwelling_type=4 if q5==5
recode dwelling_type (0=5)
label var dwelling_type "Dwelling Type"
label define dwelling_typelabel 1 "Trailer or mobile home" 2 "Apartment/duplex/condominum" 3 "Detached, single family home" 4 "Attached single family home" 5 "Other"
label values dwelling_type dwelling_typelabel
drop q5


rename q6_1 poor_insulation
rename q6_2 drafty
rename q6_3 mold
rename q6_4 holes
rename q6_5 plumbing
rename q6_6 exposed_sockets
rename q6_7 nonworking_stove_fridge
rename q6_8 broken_heating
rename q6_9 broken_ac

gen dwelling_condition=0
replace dwelling_condition=1 if poor_insulation==1
replace dwelling_condition=2 if drafty==1
replace dwelling_condition=3 if mold==1
replace dwelling_condition=4 if holes==1
replace dwelling_condition=5 if plumbing==1
replace dwelling_condition=6 if exposed_sockets==1
replace dwelling_condition=7 if nonworking_stove_fridge==1
replace dwelling_condition=8 if broken_heating==1
replace dwelling_condition=9 if broken_ac==1
label var dwelling_condition "Dwelling Condition"
label define dwelling_conditionlabel 0 "None" 1 "Poor Insulation" 2 "Drafty" 3 "Moldy" 4 "Holes" 5 "Plumbing Problems" 6 "Exposed Sockets" 7 "Non-Working Stove/Fridge" 8 "Broken Heating" 9 "Broken AC" 
label values dwelling_condition dwelling_conditionlabel



rename q9 hhbill_year
rename q11 hhbill_month
rename q12 notice_year
rename q16 notice_month
rename q13 disconnect_year
rename q17 disconnect_month
rename q19 electronic_device

rename q24_1 wap_yr
rename q24_2 liheap_yr
rename q24_3 snap_yr
rename q24_4 tanf_yr
rename q24_5 ssi_yr
rename q24_6 medicaid_yr
rename q24_7 vet_benefits_yr
rename q24_8 unemp_insurance_yr
rename q24_9 covid_stimulus_yr


gen gov_assistance_year=0
replace gov_assistance_year=1 if wap==1
replace gov_assistance_year=2 if liheap==1
replace gov_assistance_year=3 if snap==1
replace gov_assistance_year=4 if tanf==1
replace gov_assistance_year=5 if ssi==1
replace gov_assistance_year=6 if medicaid==1
replace gov_assistance_year=7 if vet_benefits==1
replace gov_assistance_year=8 if unemp_insurance==1
replace gov_assistance_year=9 if covid_stimulus==1
label var gov_assistance_year "Government Assistance"
label define gov_assistance_yearlabel 0 "None" 1 "WAP" 2 "LIHEAP" 3 "SNAP" 4 "TANF" 5 "SSI" 6 "Medicaid" 7 "Veterans Benefits" 8 "Unemployment Insurance" 9 "COVID-19 Stimulus"
label values gov_assistance_year gov_assistance_yearlabel


rename q25_1a wap_month
rename q25_2a liheap_month
rename q25_3a snap_month
rename q25_4a tanf_month
rename q25_5a ssi_month
rename q25_6a medicaid_month
rename q25_7a vet_benefits_month
rename q25_8a unemp_insurance_month


gen gov_assistance_month=0
replace gov_assistance_month=1 if wap_month==1
replace gov_assistance_month=2 if liheap_month==1
replace gov_assistance_month=3 if snap_month==1
replace gov_assistance_month=4 if tanf_month==1
replace gov_assistance_month=5 if ssi_month==1
replace gov_assistance_month=6 if medicaid_month==1
replace gov_assistance_month=7 if vet_benefits_month==1
replace gov_assistance_month=8 if unemp_insurance_month==1
label var gov_assistance_month "Government Assistance Last Month"
label define gov_assistance_monthlabel 0 "None" 1 "WAP" 2 "LIHEAP" 3 "SNAP" 4 "TANF" 5 "SSI" 6 "Medicaid" 7 "Veterans Benefits" 8 "Unemployment Insurance"
label values gov_assistance_month gov_assistance_monthlabel


rename q29 covid_diagnosis
rename q30 covid_employment
rename q31_month month_unemployed
rename q31_day day_unemployed
rename q32 covid_insurance
rename q33 covid_rent_mortgage
rename q34 covid_foregoexpenses
rename q35 covid_rent
rename q36 covid_feedfamily
rename q37 covid_medicalcare

gen age=2020-birthyr


svyset caseid [pweight=weight]


**Cleaning**
recode electronic_device (2=0)
label var electronic_device "device"
label define devicelabel 0 "no" 1 "yes"
label values electronic_device devicelabel

gen homeowner = 0
replace homeowner = 0 if ownhome >1
replace homeowner = 1 if ownhome ==1
label var homeowner "own home?"
label define homeownerlabel 0 "no" 1 "yes"
label values homeowner homeownerlabel

gen employed = 0
replace employed = 0 if employ >1
replace employed = 1 if employ <3
label var employed "employed?"
label define employedlabel 0 "no" 1 "yes"
label values employed employedlabel

gen unemployed =0
replace unemployed =1 if employ==3 | employ==4
label var unemployed "unemployed"
label define unemployedlabel 0 "no" 1 "yes"
label values unemployed unemployedlabel

gen retired = 0
replace retired = 1 if employ ==5
replace retired = 0 if (employ <5|employ >5)
label var retired "retired?"
label define retiredlabel 0 "no" 1 "yes"
label values retired retiredlabel

svy: tab electronic_device
label def device 0 "Not use device" 1 "Use device"
label val electronic_device device 

svy: tab covid_employment
gen covid_lostjobhours = 1 if covid_employment==1 | covid_employment==3 | covid_employment==4
replace covid_lostjobhours=0 if covid_lostjobhours==. & covid_employment!=.
label define lostjob 1 "Loss job or reduced hours" 0 "Retained job"
label val covid_lostjobhours lostjob

recode hhbill_year(2=0)
label define hhbill 0 "no" 1 "yes"
label val hhbill_year hhbill
replace hhbill_year=0 if hhbill_year !=1

recode covid_foregoexpenses(2=0)
label define covid_forego 0 "no" 1 "yes"
label val covid_foregoexpenses covid_forego

recode hh_under5 (1=0) (2=1) (3=2) (4=3) (5=4)
replace hh_under5=0 if hh_under5 !=1 & hh_under5 !=2 & hh_under5 !=3 & hh_under5 !=4
label define hh_under5_label 0 "0" 1 "1" 2 "2" 3 "3" 4 "more than 3"
label val hh_under5 hh_under5_label

recode hh_disability (1=0) (2=1) (3=2) (4=3) (5=4)
replace hh_disability=0 if hh_disability !=1 & hh_disability !=2 & hh_disability !=3 & hh_disability !=4
label define hh_disability_label 0 "0" 1 "1" 2 "2" 3 "3" 4 "more than 3"
label val hh_disability hh_disability_label

replace electronic_device=0 if electronic_device !=1

gen white=0
replace white=1 if race==1

gen black=0
replace black=1 if race==2

gen hispanic=0
replace hispanic=1 if race==3

gen other=0
replace other=1 if race>3

gen female=0
replace female=1 if gender==2


*lost insurance due to COVID*
gen covid_lostinsurance=0
replace covid_lostinsurance=1 if covid_insurance <3
label define lostinsurance 0 "Did not lose insurance" 1 "Lost Insurance"
lab val covid_lostinsurance lostinsurance



*****Making the 1 month skip questions include the full data set --> set equal to 0 if you didn't answer or answered no, 1 if yes ... Also filling in the year values that do not include the entire data set***
replace hhbill_month=0 if hhbill_month !=1
label define hhbill_label 0 "no" 1 "yes"
label values hhbill_month hhbill_label

replace notice_year=0 if notice_year !=1 & notice_year !=2 & notice_year !=3 & notice_year !=4 & notice_year !=5 & notice_year !=6 & notice_year !=7 & notice_year !=8 & notice_year !=9 & notice_year !=10 & notice_year !=11 & notice_year !=12

replace notice_month=0 if notice_month !=1
label define notice_label 0 "no" 1 "yes"
label values notice_month notice_label

replace disconnect_year=0 if disconnect_year !=1 & disconnect_year !=2 & disconnect_year !=3 & disconnect_year !=4 & disconnect_year !=5 & disconnect_year !=6 & disconnect_year !=7 & disconnect_year !=8 & disconnect_year !=9 & disconnect_year !=10 & disconnect_year !=11 & disconnect_year !=12

replace disconnect_month=0 if disconnect_month !=1
label define disconnect_label 0 "no" 1 "yes"
label values disconnect_month disconnect_label

replace unemp_insurance_yr=0 if unemp_insurance_yr !=1
label define unemp_insurance_yrlabel 0 "no" 1 "yes"
label values unemp_insurance_yr unemp_insurance_yrlabel

replace unemp_insurance_month=0 if unemp_insurance_month !=1
label define unemp_insurance_label 0 "no" 1 "yes"
label values unemp_insurance_month unemp_insurance_label

replace poor_insulation=0 if poor_insulation !=1
label define poorinsulation_label 0 "no" 1 "yes"
label values poor_insulation poorinsulation_label

recode covid_diagnosis (1=3) (3=1)
label define covid_diagnosis_label 1 "No COVID Symptoms" 2 "Symptoms but no test" 3 "Positive test"
label values covid_diagnosis covid_diagnosis_label





****FPL levels (numeric)****
****THIS MAKES OUR OUR BELOW 100, 100-150, AND 150-200 CATEGORIES USABLE
gen hhinc = "100% FPL or below" if hh1_income==1
forvalues i = 2(1)8 {
replace hhinc = "100% FPL or below" if hh`i'_income==1
}
forvalues i = 1(1)8 {
replace hhinc = "100%-150% FPL" if hh`i'_income==2 | hh`i'_income==3
}
forvalues i = 1(1)8 {
replace hhinc = "150%-200% FPL" if hh`i'_income==4 | hh`i'_income==5
}
encode hhinc, gen(fpl)


***Income levels by FPL***
gen under_100_fpl=0
replace under_100_fpl=1 if fpl==1

gen FPL_100_150=0
replace FPL_100_150=1 if fpl==2

gen FPL_150_200=0
replace FPL_150_200=1 if fpl==3


**COVID - no symptoms versus symptoms or diagnosis
gen covid_symptoms=0 if covid_diagnosis==1
replace covid_symptoms=1 if covid_diagnosis==2 | covid_diagnosis ==3
label define covid_symptoms_label 0 "No symptoms or diagnosis" 1 "Symptoms or diagnosis"
label values covid_symptoms covid_symptoms_label

****Covid diagnosis****
gen positive_covid_test = 0 if covid_diagnosis ==1 | covid_diagnosis ==2
replace positive_covid_test =1 if covid_diagnosis ==3
label define positivecovid_label 0 "No positive test" 1 "Positive test"
label values positive_covid_test positivecovid_label

*****Merge zip code, urban vs rural percentage*****
sort inputzip
merge m:1 inputzip using ${ai}/zcta_urban.dta
drop if _m==2

egen newid = group(caseid)

gen wave1=1

keep hhsize hh_under5 hh_over65 hh_disability dwelling_type dwelling_condition hhbill_year hhbill_month notice_year notice_month disconnect_year disconnect_month electronic_device covid_stimulus_yr gov_assistance_year gov_assistance_month covid_diagnosis covid_employment month_unemployed day_unemployed covid_insurance covid_rent_mortgage covid_foregoexpenses covid_rent covid_feedfamily covid_medicalcare educ age homeowner employed unemployed retired white black hispanic other female covid_lostinsurance covid_lostjobhours unemp_insurance_yr unemp_insurance_month fpl under_100_fpl FPL_100_150 FPL_150_200 covid_symptoms positive_covid_test inputzip pcturban inputstate newid wave1 weight

save C:\RA\Analysis\input\replication_survey.dta, replace

log close
